/**
 * SpringMVC restful 风格
 * 云市场控制器，控制关于云市场的一些业务逻辑操作
 * @author WangXinLei
 * @date 2014-01-19
 */
package com.chnbst.bstyun.controller.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.GoodsType;
import com.chnbst.bstyun.model.pojo.Menu;
import com.chnbst.bstyun.model.vo.GoodsTypeTreeVo;
import com.chnbst.bstyun.model.vo.GoodsTypeVo;
import com.chnbst.bstyun.model.vo.GoodsVo;
import com.chnbst.bstyun.model.vo.IndexModuleVo;
import com.chnbst.bstyun.service.GoodsService;
import com.chnbst.bstyun.service.GoodsTypeService;
import com.chnbst.bstyun.service.IndexModuleService;
import com.chnbst.bstyun.service.MenuService;

@Controller
@RequestMapping("/mall")
public class CloundMarketController {
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private MenuService menuService;
	@Autowired
	private GoodsTypeService goodsTypeService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private IndexModuleService indexModuleService;
	
	/**
	 * http://xxx.com/mall
	 * 云市场首页推荐
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView cloudmarket(HttpServletRequest request){
		//上方菜单显示
		List<Menu> menus = menuService.selectMenus();
		
		//左侧导航数据查询
		Map<String, Object> map1 = new HashMap<String, Object>();
		List<GoodsTypeTreeVo> goodsTypeList = goodsTypeService.selectGoodsTypeGrid(map1);
//		ObjectMapper objectMapper = new ObjectMapper();
//		String menuString = "";
//		try {
//			menuString = objectMapper.writeValueAsString(goodsTypeList);
//	    } catch (JsonParseException e) {
//	        e.printStackTrace();
//	    } catch (JsonMappingException e) {
//	        e.printStackTrace();
//	    } catch (IOException e) {
//	        e.printStackTrace();
//	    }
		
		List<GoodsType> gtpList = goodsTypeService.selectP();
		
		StringBuffer menuString = new StringBuffer("");
		for(int i=0;i<gtpList.size();i++){
			GoodsType gp = gtpList.get(i);
			menuString.append("<li><a href='/mall/dhlist/"+gp.getTypeId()+"'>"+gp.getTypeName()+"</a>");
			List<GoodsType> gtcList = goodsTypeService.selectC(gp.getTypeId());
			if(gtcList.size() != 0){
				menuString.append("<ul ");
				for(int j=0;j<gtcList.size();j++){
					GoodsType gc = gtcList.get(j);
					menuString.append("<li><a href='/mall/dhlist/"+gc.getTypeId()+"'>"+gc.getTypeName()+"</a>");
					List<GoodsType> list1 = goodsTypeService.selectC(gc.getTypeId());
					if(list1.size() != 0){
						menuString.append("<ul >");
						for(int k=0;k<list1.size();k++){
							GoodsType g = list1.get(k);
							menuString.append("<li><a href='/mall/dhlist/"+g.getTypeId()+"'>"+g.getTypeName()+"</a></li>");
						}
						menuString.append("</ul>");
					}
					menuString.append("</li>");
				}
				menuString.append("</ul>");
			}
			menuString.append("</li>");
		}
		
		
		//最新应用
		List<IndexModuleVo> indexModuleList = indexModuleService.selectIndexModule();
		List<GoodsVo> goodsVo = goodsService.selectGoodsByIndexModule();

		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.addObject("goodsTypeList", goodsTypeList);
		mav.addObject("indexModuleList", indexModuleList);
		mav.addObject("goodsVo", goodsVo);
		mav.addObject("menuString", menuString);
		mav.addObject("requestURI", request.getRequestURI());
		mav.setViewName("mall/cloudmarket");
		mav.addObject("treeNodeId", "null");
		return mav;
	}
		
	/**
	 * http://xxx.com/mall/dhlist
	 * 点击导航产品列表页面
	 */
	@RequestMapping(value="/dhlist/{id}",method=RequestMethod.GET)
	public ModelAndView dhlist(@PathVariable String id,HttpServletRequest request){
		//上方菜单显示
		List<Menu> menus = menuService.selectMenus();
		
		//左侧导航数据查询
		Map<String, Object> map = new HashMap<String, Object>();
		List<GoodsTypeTreeVo> goodsTypeTreeVoList = goodsTypeService.selectGoodsTypeGrid(map);

//		//树的生成
//		ObjectMapper objectMapper = new ObjectMapper();
//		String menuString = "";
//		try {
//			menuString = objectMapper.writeValueAsString(goodsTypeTreeVoList);
//	    } catch (JsonParseException e) {
//	        e.printStackTrace();
//	    } catch (JsonMappingException e) {
//	        e.printStackTrace();
//	    } catch (IOException e) {
//	        e.printStackTrace();
//	    }
		
		String typeId = id;
		IndexModuleVo indexModuleVo = indexModuleService.selectIndexModuleById(id);
		if(indexModuleVo != null){
			typeId = indexModuleVo.getTypeId();
		}
		
		GoodsType goodtype = goodsTypeService.selectGoodsTypeVoInfo(typeId);
		List<GoodsType> gtpList = goodsTypeService.selectP();
		String openid = id;
		if(!goodtype.getParentId().equals("-1")){
			GoodsType g1 = goodsTypeService.selectGoodsTypeVoInfo(goodtype.getParentId());
			openid = g1.getTypeId();
			if(!g1.getParentId().equals("-1")){
				GoodsType g2 = goodsTypeService.selectGoodsTypeVoInfo(g1.getParentId());
				openid = g2.getTypeId();
				if(!g2.getParentId().equals("-1")){
					GoodsType g3 = goodsTypeService.selectGoodsTypeVoInfo(g2.getParentId());
					openid = g3.getTypeId();
				}
			}
		}
		
		StringBuffer menuString = new StringBuffer("");
		for(int i=0;i<gtpList.size();i++){
			GoodsType gp = gtpList.get(i);
			menuString.append("<li><a href='/mall/dhlist/"+gp.getTypeId()+"'>"+gp.getTypeName()+"</a>");
			List<GoodsType> gtcList = goodsTypeService.selectC(gp.getTypeId());
			menuString.append("<ul ");
			if(gp.getTypeId().equals(openid)){
				menuString.append("class='open'");
			}
			menuString.append(">");
			if(gtcList.size() != 0){
				for(int j=0;j<gtcList.size();j++){
					GoodsType gc = gtcList.get(j);
					menuString.append("<li><a href='/mall/dhlist/"+gc.getTypeId()+"'>"+gc.getTypeName()+"</a>");
					List<GoodsType> list1 = goodsTypeService.selectC(gc.getTypeId());
					if(list1.size() != 0){
						menuString.append("<ul >");
						for(int k=0;k<list1.size();k++){
							GoodsType g = list1.get(k);
							menuString.append("<li><a href='/mall/dhlist/"+g.getTypeId()+"'>"+g.getTypeName()+"</a></li>");
						}
						menuString.append("</ul>");
					}
					menuString.append("</li>");
				}
			}
			menuString.append("</ul>");
			menuString.append("</li>");
		}
		
		//显示所点击导航分类下的产品
		map.put("typeId", typeId);
		List<GoodsVo> goodsVo = goodsService.selectGoodsVoByTypeId(map);
		
		//查询 所点击导航分类的信息
		GoodsTypeVo goodsTypeVo = goodsTypeService.selectGoodsTypeVoInfo(typeId);
		
		String typeName = goodsTypeVo.getTypeName();
		if(indexModuleVo != null){
			typeName = indexModuleVo.getModuleName();
		}
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("menus", menus);
		mav.addObject("menuString", menuString);
		mav.addObject("goodsVo", goodsVo);
		mav.addObject("typeName", typeName);
		mav.addObject("requestURI", request.getRequestURI());
		mav.setViewName("mall/goodsList");
		mav.addObject("treeNodeId", typeId);
		return mav;
	}
	
	/**
	 * http://xxx.com/mall/dhdetail
	 * 产品详情页面
	 */
	@RequestMapping(value="/dhdetail/{id}",method=RequestMethod.GET)
	public ModelAndView dhdetail(@PathVariable String id,HttpServletRequest request){
		
		//上方菜单显示
		Map<String, Object> map = new HashMap<String, Object>();
		List<Menu> menus = menuService.selectMenus();
		
		//左侧导航数据查询
		List<GoodsTypeTreeVo> goodsTypeTreeVoList = goodsTypeService.selectGoodsTypeGrid(map);
		map.put("goodsTypeTreeVoList", goodsTypeTreeVoList);

//		//树的生成
//		ObjectMapper objectMapper = new ObjectMapper();
//		String menuString = "";
//		try {
//			menuString = objectMapper.writeValueAsString(goodsTypeTreeVoList);
//	    } catch (JsonParseException e) {
//	        e.printStackTrace();
//	    } catch (JsonMappingException e) {
//	        e.printStackTrace();
//	    } catch (IOException e) {
//	        e.printStackTrace();
//	    }
		
		GoodsVo gvo = goodsService.selectGoodsVoByGoodsId(id);
		
		GoodsType goodtype = goodsTypeService.selectGoodsTypeVoInfo(gvo.getTypeId());
		List<GoodsType> gtpList = goodsTypeService.selectP();
		String openid = gvo.getTypeId();
		if(!goodtype.getParentId().equals("-1")){
			GoodsType g1 = goodsTypeService.selectGoodsTypeVoInfo(goodtype.getParentId());
			openid = g1.getTypeId();
			if(!g1.getParentId().equals("-1")){
				GoodsType g2 = goodsTypeService.selectGoodsTypeVoInfo(g1.getParentId());
				openid = g2.getTypeId();
				if(!g2.getParentId().equals("-1")){
					GoodsType g3 = goodsTypeService.selectGoodsTypeVoInfo(g2.getParentId());
					openid = g3.getTypeId();
				}
			}
		}
		
		StringBuffer menuString = new StringBuffer("");
		for(int i=0;i<gtpList.size();i++){
			GoodsType gp = gtpList.get(i);
			menuString.append("<li><a href='/mall/dhlist/"+gp.getTypeId()+"'>"+gp.getTypeName()+"</a>");
			List<GoodsType> gtcList = goodsTypeService.selectC(gp.getTypeId());
			menuString.append("<ul ");
			if(gp.getTypeId().equals(openid)){
				menuString.append("class='open'");
			}
			menuString.append(">");
			if(gtcList.size() != 0){
				for(int j=0;j<gtcList.size();j++){
					GoodsType gc = gtcList.get(j);
					menuString.append("<li><a href='/mall/dhlist/"+gc.getTypeId()+"'>"+gc.getTypeName()+"</a>");
					List<GoodsType> list1 = goodsTypeService.selectC(gc.getTypeId());
					if(list1.size() != 0){
						menuString.append("<ul >");
						for(int k=0;k<list1.size();k++){
							GoodsType g = list1.get(k);
							menuString.append("<li><a href='/mall/dhlist/"+g.getTypeId()+"'>"+g.getTypeName()+"</a></li>");
						}
						menuString.append("</ul>");
					}
					menuString.append("</li>");
				}
			}
			menuString.append("</ul>");
			menuString.append("</li>");
		}
		int isHaveYear = 1; //计算规则有年限
		if(!gvo.getFeeFormula().contains("年限")) {
			isHaveYear = 0; //计算规则无年限
		}
				
		ModelAndView mav = new ModelAndView();
		mav.addObject("menus", menus);
		mav.addObject("menuString", menuString);
		mav.addObject("goodsVo", gvo);
		mav.addObject("requestURI", request.getRequestURI());
		mav.addObject("isHaveYear", isHaveYear);
		mav.setViewName("mall/goodsDetail");
		mav.addObject("treeNodeId", gvo.getTypeId());
		return mav;
	}
}
